package com.lsh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsh.pojo.DiscussPost;
import com.lsh.pojo.User;
import com.lsh.service.impl.DiscussPostServiceImpl;
import com.lsh.service.impl.UserServiceImpl;
import com.lsh.utils.LimitUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘书豪
 * @since 2022-07-22
 */
@Controller
@RequestMapping({"/discussPost","/"})
public class DiscussPostController {
    @Resource
    DiscussPostServiceImpl discussPostService;
    @Resource
    UserServiceImpl userService;
    @RequestMapping({"/index","/"})
    public String toIndex(Model model, LimitUtil limitUtil){
        limitUtil.setTotal(discussPostService.getTotal());
        limitUtil.setSize(10);
        List<DiscussPost> discussPosts = discussPostService.selectPage(limitUtil.getCurrent(),limitUtil.getSize());
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (DiscussPost discussPost : discussPosts) {
            Map<String,Object> map = new HashMap<>();
            map.put("discuss",discussPost);
            User user = userService.getById(discussPost.getUserId());
            map.put("user",user);
            list.add(map);
        }
        model.addAttribute("discussPosts",list);
        return "index";
    }
}
